package cn.iocoder.yudao.module.enterprise.service.model;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.model.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.model.ModelConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.emissionsource.EmissionSourceDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.model.ModelDO;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.model.ModelEmissionDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.emissionsource.EmissionSourceMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.model.ModelEmissionMapper;
import cn.iocoder.yudao.module.enterprise.dal.mysql.model.ModelMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.MODEL_NOT_EXISTS;

/**
 * 核算模型 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ModelServiceImpl implements ModelService {

    @Resource
    private ModelMapper modelMapper;
    @Resource
    private EmissionSourceMapper emissionSourceMapper;
    @Resource
    private ModelEmissionMapper modelEmissionMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createModel(ModelCreateReqVO createReqVO) {
        // 插入
        ModelDO model = ModelConvert.INSTANCE.convert(createReqVO);
        modelMapper.insert(model);
        // 返回
        return model.getId();
    }

    @Override
    public void updateModel(ModelUpdateReqVO updateReqVO) {
        // 校验存在
        validateModelExists(updateReqVO.getId());
        // 更新
        ModelDO updateObj = ModelConvert.INSTANCE.convert(updateReqVO);
        modelMapper.updateById(updateObj);
    }

    @Override
    public void deleteModel(Long id) {
        // 校验存在
        validateModelExists(id);
        // 删除
        modelMapper.deleteById(id);
    }

    private void validateModelExists(Long id) {
        if (modelMapper.selectById(id) == null) {
            throw exception(MODEL_NOT_EXISTS);
        }
    }

    @Override
    public ModelDO getModel(Long id) {
        return modelMapper.selectById(id);
    }

    @Override
    public PageResult<ModelDO> getModelPage(ModelPageReqVO pageReqVO) {
        PageResult<ModelDO> pageResult = modelMapper.selectPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return new PageResult<>(pageResult.getTotal());
        }

        // 拼接创建者信息
        List<ModelDO> list = pageResult.getList();
        list = list.stream().map(e -> {
            AdminUserRespDTO user = adminUserApi.getUser(Long.parseLong(e.getUpdater())).getCheckedData();
            e.setUpdater(user.getNickname());
            return e;
        }).collect(Collectors.toList());

        pageResult.setList(list);

        return pageResult;
    }

    @Override
    public PageResult<ModelEmissionRespVO> getModelEmissionPage(ModelEmissionPageReqVO pageReqVO) {
        PageResult<ModelEmissionDO> page = modelEmissionMapper.selectPage(pageReqVO);
        PageResult<ModelEmissionRespVO> pageResult = ModelConvert.INSTANCE.convertPage02(page);

        List<ModelEmissionRespVO> list = pageResult.getList();
        list = list.stream().map(e -> {
            EmissionSourceDO emissionSource = emissionSourceMapper.selectById(e.getEmissionId());
            e.setName(emissionSource.getName())
                    .setFacility(emissionSource.getFacility())
                    .setGhgClassification(emissionSource.getGhgClassification())
                    .setIsoClassification(emissionSource.getIsoClassification())
                    .setGhgCategory(emissionSource.getGhgCategory())
                    .setIsoCategory(emissionSource.getIsoCategory())
                    .setDataUnit(emissionSource.getDataUnit());
            return e;
        }).collect(Collectors.toList());

        pageResult.setList(list);

        return pageResult;
    }

}
